import tkinter, os
from concurrent.futures import ThreadPoolExecutor
import time
from index import Load
from show import Show
from tkinter import messagebox

try:
    import pygubu
except:
    messagebox.showerror("警告", "请安装pygubu模块")

path = os.getcwd()


class Myapp():
    text_str = ''

    def __init__(self, master):
        self.window1 = tkinter.IntVar(root, value=0)
        self.window2 = tkinter.IntVar(root, value=0)
        fpath = "index.ui"
        self.builder = builder = pygubu.Builder();

        builder.add_from_file(fpath);
        mainwindow = builder.get_object('Frame_5', master)
        bk = builder.get_object("Button_1")
        bk['command'] = self.on_get_link
        self.link = builder.get_object('ilink')
        self.cookie = builder.get_object('icookies')
        # host = builder.get_object("gethb1")
        # self.list1 = builder.get_object("list1")
        # self.but3 = builder.get_object("but3")
        # self.but3['state'] = "disable";
        # host.bind("<Double-Button-1>",self.show_msg)
        self.root1 = master
        master.bind("<Alt-m>", self.quits)
        master.minsize(335, 329)
        master.maxsize(335, 329)
        master.title("暗夜小说获取器")
        master.withdraw()
        master.update()
        self.root1.deiconify()
        self.workpool = ThreadPoolExecutor(max_workers=2)
        builder.connect_callbacks(self)
        # self.text_str = "欢迎使用\n"
        # self.list1.after(1000, self.update_text,self.text_str)

    def loads(self):
        messagebox.askquestion("欢迎使用", "欢迎使用本软件")

    def on_get_link(self):
        link = self.link.get()
        cookies = self.cookie.get('0.0', 'end')
        # app = tkinter.Toplevel()
        data = Load(cookies)
        data = data.load(link)
        # print(data)
        Show(self.root1, data[0], data[1])


    def quits(self, event):
        messagebox.showerror("", "您准备退出了")


"""
http://m.anyew.cn/chapter/15088/1.html
"""
"""
PHPSESSID=asgk348npopisq5p4l0jmesqe2; Hm_lvt_50b4e823049b69a49522a7c3df9b4d7c=1529912563,1529912773,1529936808,1530027468; uuid=7d463f69-c610-7cb6-917e-39397a8c921d; first_index=1; _stat=eyJpbmRleF91bmlxdWUiOjE1MzAwMjc0NjYsImluZGV4X21fdW5pcXVlIjoxNTMwMDI3NDY2LCJpbmRleF9mX3VuaXF1ZSI6MTUzMDAyNzQ3NX0%3D; back_url_sign=1; back_url=http%3A%2F%2Fm.anyew.cn%2Fuser; pauserid=8989417; pausername=aa1522528613; papasswordhash=738928a2de4b5697bdf3b5e67620e61e; Hm_lpvt_50b4e823049b69a49522a7c3df9b4d7c=1530027496; Hm_lpvt_22457af4ec023e0e8bbf351791a5c811=1530027495; Hm_lvt_22457af4ec023e0e8bbf351791a5c811=1530027495; sexstyle=1
"""

if __name__ == "__main__":
    root = tkinter.Tk();
    mainwindow = Myapp(root)
    root.mainloop()
